package com.buka.recipe.food.controller;

import com.alibaba.fastjson2.JSONObject;
import com.buka.recipe.common.result.Result;
import com.buka.recipe.common.result.ResultGenerator;
import com.buka.recipe.food.entity.Food;
import com.buka.recipe.food.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/food")
public class FoodController {

    private final FoodService foodService;

    @Autowired
    public FoodController(FoodService foodService) {
        this.foodService = foodService;
    }

    @PostMapping("/add")
    public Result<?> add(@RequestBody JSONObject params) {
        String title = params.getString("title");
        if (!StringUtils.hasText(title)) {
            return ResultGenerator.genFailResult("标题不能为空");
        }
        String specs = params.getString("specs");
        if (!StringUtils.hasLength(specs)) {
            return ResultGenerator.genFailResult("规格不能为空");
        }
        Food food = new Food();
        food.setTitle(title);
        food.setDescription(params.getString("description"));
        food.setSpecs(specs);
        foodService.save(food);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/del")
    public Result<?> del(@RequestBody JSONObject params) {
        Integer id = params.getInteger("id");
        Food food = foodService.getFoodById(id);
        if (food == null) {
            return ResultGenerator.genFailResult("食物不存在");
        }
        foodService.delete(food);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/list")
    public Result<?> list(@RequestParam(required = false) String title,
                          @RequestParam(required = false, defaultValue = "0") Integer current,
                          @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Page<Food> foods = foodService.getAll(title, current, pageSize);
        return ResultGenerator.genSuccessResult(foods);
    }

    @PostMapping("/update")
    public Result<?> update(@RequestBody JSONObject params) {
        Integer id = params.getInteger("id");
        Food food = foodService.getFoodById(id);
        if (food == null) {
            return ResultGenerator.genFailResult("食物不存在");
        }
        String title = params.getString("title");
        if (!StringUtils.hasText(title)) {
            return ResultGenerator.genFailResult("标题不能为空");
        }
        if (!food.getTitle().equals(title)) {
            food.setTitle(title);
        }
        String specs = params.getString("specs");
        if (!StringUtils.hasLength(specs)) {
            return ResultGenerator.genFailResult("规格不能为空");
        }
        if (!food.getSpecs().equals(specs)) {
            food.setSpecs(specs);
        }
        food.setDescription(params.getString("description"));
        foodService.save(food);
        return ResultGenerator.genSuccessResult();
    }

}
